package com.tbit.uqbike.service.base.impl;

import com.tbit.uqbike.object.pojo.Account;
import com.tbit.uqbike.object.pojo.AccountServiceConfig;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.service.base.AuthService;
import com.tbit.uqbike.webmanager.dao.core.AccountDao;
import com.tbit.uqbike.webmanager.dao.core.AccountServiceConfigDao;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class AuthServiceImpl implements AuthService {

    @Resource
    private AccountDao accountDao;
    @Resource
    private AccountServiceConfigDao accountServiceConfigDao;


    /**
     * @description: 初始化账户的客服电话号码进新架构
     * @author: mo.shanyong
     * @date: 2025/1/6 15:16
     */
    @Async
    @Override
    public void initAccountTelToNew(LoginInfo info) {
        Integer startId = 0;
        Integer limit = 500;
        List<AccountServiceConfig> serviceConfigList = new ArrayList<>();
        Date now = new Date();
        log.info("开始初始化账户的客服电话进新架构");
        do {
            List<Account> accountByLimit = accountDao.getAccountByLimit(startId, limit);
            if (CollectionUtils.isEmpty(accountByLimit)){
                if (CollectionUtils.isNotEmpty(serviceConfigList)){
                    accountServiceConfigDao.insertBatch(serviceConfigList);
                    serviceConfigList.clear();
                }
                break;
            }
            for (Account account : accountByLimit) {
                if (StringUtils.isBlank(account.getPhone())){
                    continue;
                }
                List<String> telList = Arrays.asList(account.getPhone().split("[;；]")).stream().map(tel ->{
                    String[] split = tel.split("\\（(.*?)\\）");
                    String s = StringUtils.isNotEmpty(split[0]) ? split[0] : split[1];
                    String[] split1 = s.split("\\((.*?)\\)");
                    String s1 = StringUtils.isNotBlank(split1[0]) ? split1[0] : split1[1];
                    String[] split2 = s1.split("\\（(.*?)");
                    String s2 = StringUtils.isNotBlank(split2[0]) ? split2[0] : split2[1];
                    String[] split3 = s2.split("\\((.*?)");
                    return StringUtils.isNotBlank(split3[0]) ? split3[0] : split3[1];
                }).collect(Collectors.toList());
                List<String> remarks = telList.stream()
                        .map(s -> "如遇客服高峰期线路繁忙，请耐心等待")
                        .collect(Collectors.toList());
                AccountServiceConfig serviceConfig = new AccountServiceConfig();
                serviceConfig.setAccountId(account.getAccountId());
                serviceConfig.setUpdateTime(now);
                serviceConfig.setCreateTime(now);
                serviceConfig.setStartTime("00:00");
                serviceConfig.setEndTime("23:59");
                serviceConfig.setTelephones(StringUtils.join(telList,','));
                serviceConfig.setRemark(StringUtils.join(remarks,"|||"));
                serviceConfig.setCreateUserId(info.getAccountUserId());
                serviceConfigList.add(serviceConfig);
            }
            if (CollectionUtils.isNotEmpty(serviceConfigList) && serviceConfigList.size() >= 500){
                accountServiceConfigDao.insertBatch(serviceConfigList);
                serviceConfigList.clear();
            }
            startId = accountByLimit.get(accountByLimit.size() - 1).getAccountId();
        }while (true);
        log.info("初始化账户的客服电话进新架构结束");
    }
}
